import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_high_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_select_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/device_tree/view.dart';
import 'package:safetymonitor/app/components/dialog/exit_tips.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/core/utils/customEndDrawer.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';
import 'package:safetymonitor/app/core/values/gaps.dart';
import 'package:safetymonitor/app/data/entities/home/risk_tree_entity.dart';
import '../../../app/components/time/MultiWheelTimePickerTimePeriod.dart';
import 'controller.dart';

///设备报备
class MaintenancePage extends StatelessWidget {
  MaintenancePage({super.key});

  final controller = Get.put(MaintenanceController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (controller) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            appBar: CustomAppBar(
              titleText: '设备报备',
              actions: [
                Container(
                  margin: EdgeInsets.only(top: 10.w, right: 10.w),
                  padding: EdgeInsets.only(left: 5.w, top: 2.w, right: 5.w, bottom: 4.w),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4.w),
                    color: Colors.blue,
                  ),
                  child: Row(
                    children: [
                      Text(
                        '保存',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 12.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ],
                  ),
                ).clickOnce(() {
                  //1.保存接口
                  controller.submit();
                })
              ],
            ),
            body: Column(
              children: [
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
                  child: Text(
                    '因传感器标校、设备断电、维修或挪移等特殊情况的提前报备',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: const Color(0xFFED1212),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    color: const Color(0xFFF7FBFE),
                    padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
                    child: ListView(
                      children: [
                        CustomTitleChartPage(
                          title: '设备',
                          child: _tobWidget(),
                        ),
                        AppGaps.vGap16,
                        CustomTitleChartPage(
                          titleRightWidget: _selectTime(),
                          title: '报备记录',
                          child: _bottomWidget(),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            endDrawer: _filterWidget(),
          );
        });
  }

  Widget _tobWidget() {
    return Column(
      children: [
        CustomSelectPage(
          isRequired: false,
          selectText: controller.queryParams?.nodeName ?? '',
          title: '设备：',
          onCallBack: () {
            controller.onFilterButtonPressed();
          },
        ),
        CustomInputPage(
          title: '报备人：',
          isRequired: false,
          isEnabled: false,
          text: UserManager.getUser()?.nickname,
          onCallBack: (value) {},
        ),
        CustomSelectPage(
          isEnabled: true,
          isRequired: false,
          selectText: controller.startDate == '' ? '' : '${controller.startDate}至${controller.endDate}',
          title: '报备时间段选择',
          hintText: '请选择',
          onCallBack: () {
            // controller.selectTime();
            MultiWheelTimePickerTimePeriod.show(
              initialStartDate: controller.startDate,
              initialEndDate: controller.endDate,
              // initialDate: controller.startDate,
              pattern: 'yyyy-MM-dd HH:mm',
              // visibleUnits: ['year', 'month', 'day', 'hour', 'minute'],
              // disabledDate:  DateTime(2025, 1, 3),// 禁用的日期限制
              // onConfirm: (start) {
              //   controller.startDate = start;
              //   controller.endDate = start;
              //   controller.update();
              //   print("====开始时间: $start");
              //   print("====结束时间: $start");
              // },
              onStartEndConfirm: (start, end) {
                controller.startDate = start;
                controller.endDate = end;
                controller.update();
                print("====开始时间: $start");
                print("====结束时间: $end");
              },
              isRange: true, // 启用时间段选择
            );
          },
        ),
        CustomHighInputPage(
            isRequired: false,
            title: '情况说明：',
            text: controller.maintenanceRecords,
            hintText: '请输入情况说明~',
            onCallBack: (value) {
              controller.maintenanceRecords = value;
            }),
      ],
    );
  }

  Widget _bottomWidget() {
    return Column(
      children: List.generate(controller.getDeviceRecordsListEntityList.length, (index) {
        return Container(
            margin: EdgeInsets.only(bottom: 12.w),
            padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
            decoration: BoxDecoration(
              color: const Color(0XFFF6F8FF),
              borderRadius: BorderRadius.circular(8.w),
            ),
            child: Row(children: [
              Expanded(
                  child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                Row(
                  children: [
                    SvgPicture.asset('assets/svg/icon_device_tx.svg'),
                    AppGaps.hGap10,
                    Expanded(
                      child: Text(
                        '报备人：${controller.getDeviceRecordsListEntityList[index].operators}',
                        style: TextStyle(
                          fontSize: 16.sp,
                          color: const Color(0xFF3F425B),
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(left: 5.w, top: 2.w, right: 5.w, bottom: 4.w),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4.w),
                        color: Colors.red,
                      ),
                      child: Row(
                        children: [
                          Text(
                            '删除',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                            ),
                          ),
                        ],
                      ),
                    ).clickOnce(() {
                      Get.dialog<bool>(ExitTips(
                        content: '是否确定删除？',
                        func: (v) {
                          if (v) {
                            controller.del(controller.getDeviceRecordsListEntityList[index].id);
                          }
                        },
                      ));
                    })
                  ],
                ),
                AppGaps.vGap10,
                Row(
                  children: [
                    Text(
                      '报备维修时间：',
                      style: TextStyle(
                        fontSize: 15.sp,
                        color: const Color(0xFFC7CEE5),
                      ),
                    ),
                    Expanded(
                      child: Text(
                        '${controller.getDeviceRecordsListEntityList[index].operationTimeStart!.millTS2Str()}至${controller.getDeviceRecordsListEntityList[index].operationTimeEnd!.millTS2Str()}',
                        style: TextStyle(
                          fontSize: 15.sp,
                          color: const Color(0Xff1D3452),
                        ),
                      ),
                    ),
                  ],
                ),
                AppGaps.vGap10,
                Row(
                  children: [
                    Text(
                      '单位名：',
                      style: TextStyle(
                        fontSize: 15.sp,
                        color: const Color(0xFFC7CEE5),
                      ),
                    ),
                    Expanded(
                      child: Text(
                        '${controller.getDeviceRecordsListEntityList[index].companyName}',
                        style: TextStyle(
                          fontSize: 15.sp,
                          color: const Color(0Xff1D3452),
                        ),
                      ),
                    ),
                  ],
                ),
                AppGaps.vGap10,
                Row(
                  children: [
                    Text(
                      '提交时间：',
                      style: TextStyle(
                        fontSize: 15.sp,
                        color: const Color(0xFFC7CEE5),
                      ),
                    ),
                    Expanded(
                      child: Text(
                        '${controller.getDeviceRecordsListEntityList[index].createTime}'.millTS2Str(),
                        style: TextStyle(
                          fontSize: 15.sp,
                          color: const Color(0Xff1D3452),
                        ),
                      ),
                    ),
                  ],
                ),
                AppGaps.vGap10,
                Row(
                  children: [
                    Text(
                      '情况说明：',
                      style: TextStyle(
                        fontSize: 15.sp,
                        color: const Color(0xFFC7CEE5),
                      ),
                    ),
                    Expanded(
                      child: Text(
                        '${controller.getDeviceRecordsListEntityList[index].maintenanceRecords}',
                        style: TextStyle(
                          fontSize: 15.sp,
                          color: const Color(0Xff1D3452),
                        ),
                      ),
                    ),
                  ],
                ),
              ]))
            ]));
      }),
    );
  }

  Widget _filterWidget() {
    return CustomEndDrawer(
      centerContext: Column(
        children: [
          DeviceTreePage(
            selectedEntity: controller.selectedEntity2,
            onCallBack: (RiskTreeEntity entity) {
              controller.selectedEntity = entity;
            },
          ),
        ],
      ),
      reset: controller.reset,
      confirm: controller.confirm,
    );
  }

  Widget _selectTime() {
    return Expanded(
      flex: 3,
      child: Container(
        padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 6.w, bottom: 6.w),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8.w),
            border: Border.all(
              color: AppColors.font_color_gray.withOpacity(0.3),
              width: 1,
            )),
        child: Row(
          children: [
            SvgPicture.asset('assets/svg/icon_calendar.svg'),
            Expanded(
                child: Text(
              controller.startRecordDate,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: controller.startRecordDate == '开始时间' ? const Color(0xffA0A7B5) : const Color(0xff666666),
              ),
            )),
            AppGaps.hGap5,
            Text(
              '至',
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: AppColors.font_color_black,
              ),
            ),
            AppGaps.hGap5,
            Expanded(
                child: Text(
              controller.endRecordDate,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: controller.endRecordDate == '结束时间' ? const Color(0xffA0A7B5) : const Color(0xff666666),
              ),
            )),
          ],
        ).click(() {
          MultiWheelTimePickerTimePeriod.show(
            // pattern: 'yyyy-MM-dd HH:mm',
            // visibleUnits: ['year', 'month', 'day', 'hour', 'minute'],
            initialStartDate: controller.startRecordDate,
            initialEndDate: controller.endRecordDate,
            onStartEndConfirm: (start, end) {
              controller.startRecordDate = start;
              controller.endRecordDate = end;
              controller.deviceRecordsGetList();
            },
            isRange: true, // 启用时间段选择
          );
        }),
      ),
    );
  }
}
